//
//  MemberOneViewController.m
//  zhixinApp
//
//  Created by zhiguanzhl on 2017/11/27.
//  Copyright © 2017年 zhiguanzhl. All rights reserved.
//

#import "MemberOneViewController.h"
#import "MemlistTableViewCell.h"
#import "SelectMemberViewController.h"

@interface MemberOneViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,copy)NSString *cursor;

@end

@implementation MemberOneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"群成员";
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kSCREEN_WIDTH, kSCREEN_HEIGHT-64) style:UITableViewStylePlain];
    self.tableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    
    tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        [self loadMoreData];
    }];
    tableView.tableFooterView = [[UIView alloc] init];
     
    if (_notAdmin || _isCompany) {
        //不是管理员只能看
        [self.logaiNameArr addObject:self.owner];
        
    }
    
    [self.view addSubview:tableView];
     [self.view addSubview:[self BottmView]];
    self.cursor = @"";
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
    
}


     
- (UIView *)BottmView{
    
    UIView *footView= [[UIView alloc]initWithFrame:CGRectMake(0,kSCREEN_HEIGHT-64-72, kSCREEN_WIDTH, 72)];
    [self.view addSubview:footView];
    
    UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(15, 10, kSCREEN_WIDTH-30, 52)];
    btn.layer.cornerRadius = 52*0.5;
    btn.layer.masksToBounds = YES;
    btn.backgroundColor = [UIColor colorWithHexString:@"2c9ae8"];
    btn.titleLabel.font = [UIFont systemFontOfSize:18];
    [btn setTitle:@"确认" forState:UIControlStateNormal];
    [btn setTitleColor:KWhiteColor forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(sureReduce) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:btn];
    
    return footView;
}
- (void)loadData{
    
    EMError *error = nil;
    //群组成员需要掉这个方法
    EMCursorResult* result = [[EMClient sharedClient].groupManager getGroupMemberListFromServerWithId:self.conversationId cursor:@"" pageSize:50 error:&error];
    if (!error) {
       [self.tableView.mj_header endRefreshing];
    }
    self.cursor = result.cursor;
    
    NSArray *occupantArr = result.list;
    [self.logaiNameArr removeAllObjects];
    [self.logaiNameArr addObjectsFromArray:occupantArr];
    [self englishToChinaWithLogiNameSArr:self.logaiNameArr];
    [self.tableView reloadData];
    
}
- (void)loadMoreData{
    
    EMError *error = nil;
    //群组成员需要掉这个方法
    EMCursorResult* result = [[EMClient sharedClient].groupManager getGroupMemberListFromServerWithId:self.conversationId cursor:self.cursor pageSize:50 error:&error];
    if (!error) {
         [self.tableView.mj_footer endRefreshing];
    }
    if ([self.cursor isEqualToString:@""]) {
        // 拿到当前的上拉刷新控件，结束刷新状态
        [self.tableView.mj_footer endRefreshing];
        //没有更多数据
        [self addEmptyView];
        return;
    }
    self.cursor = result.cursor;
    NSArray *occupantArr = result.list;
    [self.logaiNameArr addObjectsFromArray:occupantArr];
    [self englishToChinaWithLogiNameSArr:self.logaiNameArr];
    [self.tableView reloadData];
    
}

- (void)englishToChinaWithLogiNameSArr:(NSMutableArray *)logainNames{
    
    NSData *data = [[NSUserDefaults standardUserDefaults]valueForKey:@"numberArr"];
    NSArray *numberSArr = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    [self.memberArr removeAllObjects];
    for (NSString *nameStr in logainNames) {
        NSString *titleName = [Tool getHeadNameWithArr:numberSArr withString:nameStr];
        
        [self.memberArr addObject:titleName];
    }
    
}
     
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.memberArr.count;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MemlistTableViewCell *membersCell = [MemlistTableViewCell cellWithTableView:tableView];
    membersCell.nickLabel.text = self.memberArr[indexPath.row];
    //    membersCell.contentLabel.text = self.groupName;
    
    NSString *name = self.memberArr[indexPath.row];
    if (name.length>2) {
        name = [name substringFromIndex:1];
    }
    membersCell.headerButton.text = name;
    
    return membersCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_notAdmin) {
        
        iToast *toast = [[iToast alloc] initWithText:@"您不是管理员，只能查看成员"];
        [toast show];
        
    }else{
        
        if (self.conversationId) {
            
            //移除群组成员
            [[EMClient sharedClient].groupManager removeOccupants:@[self.logaiNameArr[indexPath.row]] fromGroup:self.conversationId error:nil];
        }
        [self.memberArr removeObjectAtIndex:indexPath.row];
        [self.logaiNameArr removeObjectAtIndex:indexPath.row];
        
        [self.tableView reloadData];
    }
    
}

- (void)sureReduce{
    
    if (self.block) {
        self.block(self.memberArr, self.logaiNameArr);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)addEmptyView{
    UIView *view = [[UIView alloc] init];
    view.frame = CGRectMake(0,0, kSCREEN_WIDTH-30, 60);
    view.backgroundColor = KWhiteColor;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0,10, kSCREEN_WIDTH-30, 30)];
    label.text = @"暂无更多数据";
    label.textColor = [UIColor colorWithHexString:@"454545"];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:15.f];
    [view addSubview:label];
    self.tableView.tableFooterView = view;
    
}

//删除人传给后台
//- (void)reduceMembersWithGroupId:(NSString *)groupId withMembers:(NSArray *)members{
//    NSMutableDictionary * paramDict = [[NSMutableDictionary alloc] init];
//
//    [paramDict setValue: self.conversationId forKey:@"groupId"];
//    [paramDict setValue:members forKey:@"members"];
////    @WeakObj(self);
//    [ZLHXHelper postURL:@"im/addGroupUserBatch" paramDict:paramDict completion:^(NSDictionary *responseObject) {
//        if ([responseObject valueForKey:@"code"] == 0) {
//            iToast *itost = [[iToast alloc] initWithText:@"添加成功"];
//            [itost show];
//        }
//    } error:^(NSError *error) {
//
//    }];
//}


- (NSMutableArray *)memberArr{
    if (!_memberArr) {
        _memberArr = [[NSMutableArray alloc] init];
    }
    return _memberArr;
}
- (NSMutableArray *)logaiNameArr{
    if (!_logaiNameArr) {
        _logaiNameArr = [[NSMutableArray alloc] init];
    }
    return _logaiNameArr;
}

@end
